
gat = Components.classes["@mozilla.org/EventHandlers;1"].getService(Components.interfaces.nsIEventHandlers);



var myExt_urlBarListener = {
	
	QueryInterface: function(aIID)
	{
		if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
		aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
		aIID.equals(Components.interfaces.nsISupports))
			return this;
		throw Components.results.NS_NOINTERFACE;
	},

	onLocationChange: function(aProgress, aRequest, aURI)
	{
		myExtension.processNewURL(aURI);
	}
	
};

var myExtension = {
	
	oldURL: null,
	
	init: function()
	{
		// Listen for webpage loads
		gBrowser.addProgressListener(myExt_urlBarListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
	},
	
	uninit: function()
	{
		gBrowser.removeProgressListener(myExt_urlBarListener);
	},
	
	processNewURL: function(aURI)
	{
		if (aURI.spec == this.oldURL)
			return;
		
		// now we know the url is new...
		//window.open('chrome://nepomuk/content/getTags.xul', 'getTags', 'chrome,width=1,height=1');
		
		this.oldURL = aURI.spec;
		
		//Refresh the info shown on the main interface
		gat.refreshPageInfo(aURI.spec, document);
		
		//Handle the elements on SideBar
		//get the SideBar first
		//Firefox has a element with id 'sidebar' to access the SideBar
		var sidebar = document.getElementById("sidebar");
		//Get the elements on SideBar
		var commentIcon = sidebar.contentDocument.getElementById("SBcommentIcon");
		var rateLabel = sidebar.contentDocument.getElementById("SBrateLabel");
		//##This doesn't work.##
		//var tagsTree = sidebar.contentDocument.getElementById("SBtagTree");
		//tagsTree.setAttribute("datasources","chrome://nepomuk/content/tagsCache.rdf");		
		//tagsTree.builder.refresh();
		
		//Get values from the main browser window
		var theComment = document.getElementById("commentIcon").getAttribute("tooltiptext");
		var rateNumber = document.getElementById("rateLabel").getAttribute("tooltiptext");
		
		//Set the SideBar with the values
		commentIcon.setAttribute("tooltiptext", theComment);
		if (theComment=="no description")
			commentIcon.setAttribute("src", "chrome://nepomuk/content/images/writeDesc.png");
		else
			commentIcon.setAttribute("src", "chrome://nepomuk/content/images/comment.png");
		rateLabel.setAttribute("tooltiptext", rateNumber);

		var i = 0;
		for (i = 0; i < 10; i++){
			rateNumber = parseInt(rateNumber);
			var starFileName = "star";
			
			if (i < rateNumber)
				starFileName += "Light";
			else
				starFileName += "Dark";
			
			if (i % 2 == 0)
				starFileName += "L";
			else
				starFileName += "R";
			
			var starId = "SBstar"+i.toString();
			var starImg = sidebar.contentDocument.getElementById(starId);
			starImg.setAttribute('src', 'chrome://nepomuk/content/images/'+starFileName+'.png');
		}
	}
	
};

window.addEventListener("load", function() {myExtension.init()}, false);
window.addEventListener("unload", function() {myExtension.uninit()}, false); 
